﻿<?php

function fromKeywordToCode($field, $keyword)
{
    if($_SERVER['SERVER_NAME'] == "127.0.0.1"){
        $mysql_host = "127.0.0.1";
        $mysql_host_s = "127.0.0.1";
        $mysql_user = "root";
        $mysql_password = "root";
        $mysql_database = "doucube";
    }
    else if ($_SERVER["SERVER_NAME"] == "discuz.comli.com"){
        $mysql_host = "mysql6.000webhost.com";
        $mysql_host_s = "mysql6.000webhost.com";
        $mysql_user = "a2751502_user2";
        $mysql_password = "taojin1021";
        $mysql_database = "a2751502_weixin";
    }
    else {
        $mysql_host = SAE_MYSQL_HOST_M;
        $mysql_host_s = SAE_MYSQL_HOST_S;  //sae的从库
        $mysql_user = SAE_MYSQL_USER;
        $mysql_password = SAE_MYSQL_PASS;
        $mysql_database =  SAE_MYSQL_DB;
    }

	$mysql_table = "weather";
	$mysql_state = "SELECT * FROM ".$mysql_table." WHERE ".$field." = \"".$keyword."\"";
	//var_dump($mysql_state."<br>");
	$con = mysql_connect($mysql_host_s, $mysql_user, $mysql_password);
	if (!$con){
		die('Could not connect: ' . mysql_error());
	}
	mysql_query("SET NAMES 'UTF8'");
	mysql_select_db($mysql_database, $con);
	$result = mysql_query($mysql_state);
    $citylist = array(); 
    while($row = mysql_fetch_array($result))
    {
        $citylist[$row['cityName']] = $row['cityCode']; 
    }
	mysql_close($con);
    //var_dump($citylist);
    //echo("<br>");
	return $citylist;
}

function getWeather_sixdays($cityCode, $returnArray)
{
	if ($cityCode == "")
	{
		return $cityCode." 错误的城市名或者此城市不在数据库中!";
	}
	$url = "http://m.weather.com.cn/data/".$cityCode.".html";
    //echo $url."<br>";
    $output = httpGetRequest($url);
    $weather = json_decode($output, true);
	$info = $weather['weatherinfo'];
    if(!isset($info))
    {
        return;
    }
	// $city = $info['city']." ".$info['city_en'];
	// $today = $info['date_y']."  ".$info['week'];
    $weatherArray = array(); 
    $weekArray = array("日","一","二","三","四","五","六");
    $item_str = "";
    //for ($i = 1; $i <= 6; $i++) {
	for ($i = 1; $i <= 3; $i++) {
        $offset = strtotime("+".($i-1)." day");
        $xTitle = date("m月d日",$offset)." 周".$weekArray[date('w',$offset)]." ".$info['temp'.$i]." ".$info['weather'.$i]." ".$info['wind'.$i];
        $weatherArray[] =  array(
            "title" =>  $xTitle,
            "description" =>  "",
            "pic" =>  "http://discuz.comli.com/weixin/weather/icon/"."d".sprintf("%02u",$info['img'.(($i *2)-1)]).".jpg",
            "url" =>  "",
        );
        $item_str .= $xTitle."\n";
    }
    if($returnArray){
        return $weatherArray;
    }else {
        return $item_str;
    }
}

function getWeather_realTime($cityCode, $returnArray)
{
	if ($cityCode == "")
	{
		return $cityCode." 错误的城市名或者此城市不在数据库中!";
	}

	$url = "http://www.weather.com.cn/data/sk/".$cityCode.".html";
    $output = httpGetRequest($url);
    $weather = json_decode($output, true);
	$info = $weather['weatherinfo'];
    $result = "【".$info['city']."】天气实况 温度：".$info['temp']."℃ 湿度：".$info['SD']." 风速：".$info['WD'].$info['WSE']."级"."\n";
    $weatherArray = array(); 
    $weatherArray[] = array(
        "title"=>str_replace("%", "﹪", $result), "description"=>"", "pic"=>"http://discuz.comli.com/weixin/weather/icon/banner.jpg", "url" =>  "");

    if($returnArray){
        return $weatherArray;
    }else {
        return $result;
    }
}

function httpGetRequest($url)
{
	$headers = array(
		"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1",
		"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
		"Accept-Language: en-us,en;q=0.5",
		//"Accept-Encoding: gzip, deflate",
		"Referer: http://www.weather.com.cn/"
	);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
	$output = curl_exec($ch);
	curl_close($ch);

    if ($output === FALSE){
        return "cURL Error: ". curl_error($ch);
    }
    return $output;
}

?>